import { getLogin } from "@/api/login";
import { setInfo, setLoginInfo } from "@/utils/auth";
import dayjs from "dayjs";

const state = {
    userInfo: ''
}

const mutations = {
    SET_TOKEN: (state, userInfo) => {
        state.userInfo = userInfo
    }
}

const actions = {
    // user login
    login({ commit }, loginInfo) {
        return new Promise(resolve => {
            getLogin(loginInfo, function(res) {
                let userInfo = {
                    accessToken: res.accessToken,
                    userId: res.userId,
                    platform: res.platform,
                    secret: res.secret,
                    roles: res.roles
                }
                commit('SET_TOKEN', userInfo)
                setInfo(userInfo)
                setLoginInfo({
                    no: loginInfo.no,
                    pwd: loginInfo.pwd,
                    loginTime: dayjs().valueOf()
                })
                resolve(res)
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}